#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

using System.Web;

using System.Collections;
using WaveMaster.Holidays.Club.Domain;
using WaveMaster.Holidays.Club.Service;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class VesselForm : Form
    {
        Vessel m_vessel;
        User m_current_user;
        EditMode m_editmode;
        PassportService m_passportService;
        VesselService m_vesselService;
        MainForm m_mainForm;
        UserControlManageVessel m_parentForm;

        public VesselForm(string name, Vessel user, User currentUser, MainForm mainForm, UserControlManageVessel parentForm, EditMode editmode)
        {
            InitializeComponent();
            m_passportService = (PassportService)ServiceFactory.Instance().GetService(ServiceFactory.PASSPORT_SERVICE);
            m_vesselService = (VesselService)ServiceFactory.Instance().GetService(ServiceFactory.VESSEL_SERVICE);
            m_current_user = currentUser;
            m_mainForm = mainForm;
            this.Name = name;
            m_parentForm = parentForm;
            initButtonEvent();
            initComboDataSource();
            if (user.m_vesselID == 0)
            {
                clearForm();
            }
            else
            {
                m_vessel = m_vesselService.GetVesselByID(user.m_vesselID); ;
                loadUser(); 
                enableForm(editmode == EditMode.Edit);
            }
            setEditMode(editmode);
            //enableForm(false);
           
        }

        private void clearForm()
        {
            textBoxName.Text = string.Empty;
            textBoxUserCode.Text = string.Empty;
            textBoxREmark.Text = string.Empty;
            comboBoxCountry.SelectedIndex = 0;
            comboBoxFlag.SelectedIndex = 0;
            numericUpCapacity.Value = 100;
            m_vessel = new Vessel();
            clearErrorProvider();
            enableForm(true);
            setEditMode(EditMode.Create);
            textBoxUserCode.SelectAll();
        }

        private void initButtonEvent()
        {
            //toolBarButtonClear.Click += new EventHandler(toolBarButtonClear_Click);
            toolBarButtonSave.Click += new EventHandler(toolBarButtonSave_Click);
            //toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            //toolBarButtonSearch.Click += new EventHandler(toolBarButtonSearch_Click);
            toolBarButtonCancel.Click += new EventHandler(toolBarButtonCancel_Click);
        }
              
        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            setEditMode(EditMode.Edit);
            enableForm(!toolBarButtonEdit.Enabled);
        }

        void toolBarButtonCancel_Click(object sender, EventArgs e)
        {
            if (m_editmode == EditMode.Load)
                this.Close();
            else
            MessageBox.Show("Are you sure to cancel?", "Confirmation", MessageBoxButtons.YesNo,
               MessageBoxIcon.Question, new EventHandler(cancelConfirmation));
        }

        void cancelConfirmation(object sender, EventArgs e)
        {
             MessageBoxWindow msg = (MessageBoxWindow)sender;
             if (msg.DialogResult == DialogResult.Yes)
             {
                 this.Close();
             }
        }
        private void initComboDataSource()
        {
            comboBoxCountry.DataSource = m_passportService.LoadAllCountry();
            comboBoxFlag.DataSource = m_passportService.LoadAllCountry();
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            //loadAllUser();
            initComboDataSource();
            if (m_editmode == EditMode.Load || m_editmode == EditMode.Edit)
                loadUser();
        }

        private void clearErrorProvider()
        {
            errorProviderUser.SetError(textBoxUserCode, "");
            errorProviderUser.SetError(textBoxName, "");
            errorProviderUser.SetError(comboBoxCountry, "");
        }
        private bool isValidForm()
        {
            clearErrorProvider();
            bool valid_a = textBoxUserCode.Text != string.Empty;
            bool valid_b = textBoxName.Text != string.Empty;
            bool valid_c = comboBoxCountry.Text != string.Empty;
            if (!valid_a) errorProviderUser.SetError(textBoxUserCode, "User Code can not be empty");
            if (!valid_b) errorProviderUser.SetError(textBoxName, "User Name can not be empty");
            if (!valid_c) errorProviderUser.SetError(comboBoxCountry, "Country can not be empty");
            return valid_a && valid_b && valid_c;
        }

        void toolBarButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (isValidForm())
                {
                    m_vessel.m_vesselCode = textBoxUserCode.Text;
                    m_vessel.m_vesselName = textBoxName.Text;
                    m_vessel.m_country = comboBoxCountry.Text;
                    m_vessel.m_flag = comboBoxFlag.Text;
                    m_vessel.m_remark = textBoxREmark.Text;
                    m_vessel.m_capacity = Convert.ToInt16(numericUpCapacity.Value);
                    
                    if (m_vessel.m_vesselID > 0)
                    {
                        m_vessel.m_modfctnBy = m_current_user.UserID;
                        m_vesselService.UpdateVessel(m_vessel);
                    }
                    else
                    {
                        m_vessel.m_cretnBy = m_current_user.UserID;
                        m_vesselService.InserVessel(m_vessel);
                    }
                    m_parentForm.LoadAllVessel();
                  //  MessageBox.Show("Record has been saved.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    MessageBox.Show("Record has been saved", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, new EventHandler(closeForm));
                    //clearForm();
                    //loadAllUser();
                    //setEditMode(EditMode.Saved);
                   // this.Close();
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

        }

        void closeForm(object sender, EventArgs e)
        {
            this.Close();
        }

        private void setEditMode(EditMode editMode)
        {
            toolBarButtonSave.Enabled = (editMode == EditMode.Create || editMode == EditMode.Edit || editMode == EditMode.Saved || editMode == EditMode.Delete)&& m_current_user.RoleDetails(this.Name).NewFlag;

            toolBarButtonEdit.Enabled = (editMode == EditMode.Load) && m_current_user.RoleDetails(this.Name).UpdateFlag;
           
            m_editmode = editMode;
            labelEditMode.Text = "[" + editMode + "]";
        }

        private void enableForm(bool enable)
        {
            textBoxName.ReadOnly = !enable;
            textBoxUserCode.ReadOnly = !enable;
            comboBoxFlag.Enabled = enable;
            comboBoxCountry.Enabled = enable;
            textBoxREmark.ReadOnly = !enable;
            numericUpCapacity.Enabled = enable;
        }

        private void loadUser()
        {
            textBoxName.Text = m_vessel.m_vesselName;
            textBoxUserCode.Text = m_vessel.m_vesselCode;
            comboBoxCountry.Text = m_vessel.m_country;
            comboBoxFlag.Text = m_vessel.m_flag;
            textBoxREmark.Text = m_vessel.m_remark;
            numericUpCapacity.Value = m_vessel.m_capacity;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}